import unittest
from HTMLTestRunner import HTMLTestRunner
from random import randint
from time import sleep

from selenium import webdriver
from selenium.webdriver.support.select import Select


class JXC_Login_Test(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.get('http://localhost/zcb/index.asp')

        isp_list = self.driver.find_elements_by_tag_name('input')
        isp_list[0].send_keys('admin')
        isp_list[1].send_keys('admin')
        isp_list[2].click()

        self.driver.switch_to.frame('left')
        sysset_css = '.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1)'
        self.driver.find_element_by_css_selector(sysset_css).click()
        sleep(2)

    def tearDown(self):
        # 退出
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('topFrame')
        self.driver.find_element_by_link_text('安全退出').click()
        self.driver.switch_to.alert.accept()
        # 关闭浏览器
        self.driver.quit()

    def test_delete(self):
        # 删除所有单位
        danwei_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(7) > td:nth-child(1)'
        self.driver.find_element_by_css_selector(danwei_css).click()

        sleep(2)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')

        sleep(1)
        self.driver.find_element_by_id('chkall').click()
        sleep(1)
        sc = 'input.button:nth-child(2)'
        self.driver.find_element_by_css_selector(sc).click()
        sleep(1)
        self.driver.switch_to.alert.accept()
        sleep(2)
        self.assertNotIn('吨', self.driver.page_source)
        sleep(2)

    def test_add_danwei(self):
        # 增加一条单位记录
        danwei_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(7) > td:nth-child(1)'
        self.driver.find_element_by_css_selector(danwei_css).click()

        sleep(2)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')

        sleep(2)
        bt_css = 'input.button:nth-child(1)'
        self.driver.find_element_by_css_selector(bt_css).click()

        sleep(2)
        self.driver.find_element_by_name('danwei').send_keys('吨')
        self.driver.find_element_by_name('submit').click()
        self.driver.switch_to.alert.accept()
        sleep(2)
        self.assertIn('吨', self.driver.page_source)
        sleep(2)

    def test_add_lei(self):
        # 增加一个大类
        lei_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(5) > td:nth-child(1)'
        self.driver.find_element_by_css_selector(lei_css).click()

        sleep(1)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')

        sleep(1)
        addlei_css = 'input.button:nth-child(1)'
        self.driver.find_element_by_css_selector(addlei_css).click()

        sleep(1)
        self.driver.find_element_by_name('bigclass').send_keys('服装')
        self.driver.find_element_by_name('submit').click()
        self.driver.switch_to.alert.accept()
        sleep(2)
        self.assertIn('服装', self.driver.page_source)

    def test_add_xlei(self):
        # 新增一个小类（随机选取一个大类）
        xlei_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(6) > td:nth-child(1)'
        self.driver.find_element_by_css_selector(xlei_css).click()

        sleep(1)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')

        sleep(1)
        addxlei_css = 'input.button:nth-child(1)'
        self.driver.find_element_by_css_selector(addxlei_css).click()

        sleep(1)
        sel_lst = Select(self.driver.find_element_by_name('bigclass'))
        lst_count = len(sel_lst.options)
        rand = randint(0, lst_count - 1)
        sel_lst.select_by_index(rand)

        sleep(1)
        self.driver.find_element_by_name('smallclass').send_keys('添加')
        self.driver.find_element_by_name('submit').click()
        self.driver.switch_to.alert.accept()
        sleep(2)
        self.assertIn('添加', self.driver.page_source)



def test_suite():
        suite = unittest.TestSuite()
        suite.addTest(JXC_Login_Test('test_delete'))
        suite.addTest(JXC_Login_Test('test_add_danwei'))
        suite.addTest(JXC_Login_Test('test_add_lei'))
        suite.addTest(JXC_Login_Test('test_add_xlei'))
        return suite
with open('d:/r.html', 'wb') as f:
        runner = HTMLTestRunner(stream=f, title='进销存测试报告')
        runner.run(test_suite())
